{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-08T14:14:04.969039Z",
     "start_time": "2025-03-08T14:13:40.488752Z"
    }
   },
   "source": [
    "import random\n",
    "import time\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个空列表\n",
    "a = []\n",
    "\n",
    "# 使用循环生成 100,000,000 个随机数并添加到列表 a 中\n",
    "for i in range(10**8):\n",
    "    a.append(random.random())\n",
    "\n",
    "# 记录当前时间\n",
    "t1 = time.time()\n",
    "\n",
    "# 使用 Python 内置的 sum 函数计算列表 a 的和\n",
    "sum1 = sum(a)\n",
    "\n",
    "t2 = time.time()\n",
    "\n",
    "# 将列表 a 转换为 NumPy 数组\n",
    "b = np.array(a)\n",
    "\n",
    "t4 = time.time()\n",
    "\n",
    "# 使用 NumPy 的 sum 函数计算数组 b 的和\n",
    "sum3 = np.sum(b)\n",
    "\n",
    "t5 = time.time()\n",
    "\n",
    "# 打印两种方法的时间差\n",
    "print(\"Python sum 时间:\", t2 - t1)\n",
    "print(\"NumPy sum 时间:\", t5 - t4)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python sum 时间: 1.3861780166625977\n",
      "NumPy sum 时间: 0.47660207748413086\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-08T14:18:46.070704Z",
     "start_time": "2025-03-08T14:18:46.062177Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 将列表转换为 NumPy 数组\n",
    "list1 = [1, 2, 3, 4]\n",
    "oneArray = np.array(list1)\n",
    "\n",
    "print(\"oneArray 类型:\", type(oneArray))\n",
    "print(\"oneArray 内容:\", oneArray)\n",
    "\n",
    "# 创建数组的多种形式\n",
    "\n",
    "# 1. 直接传入列表的方式\n",
    "t1 = np.array([1, 2, 3])\n",
    "print(\"t1 内容:\", t1)\n",
    "print(\"t1 类型:\", type(t1))\n",
    "\n",
    "# 2. 传入 range 生成序列\n",
    "t2 = np.array(range(10))\n",
    "print(\"t2 内容:\", t2)\n",
    "print(\"t2 类型:\", type(t2))\n",
    "\n",
    "\n",
    "# 3. 使用 numpy 自带的 np.arange() 生成数组\n",
    "t3 = np.arange(0, 10, 2)\n",
    "print(\"t3 内容:\", t3)\n",
    "print(\"t3 类型:\", type(t3))\n"
   ],
   "id": "5abec6024c086cca",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "oneArray 类型: <class 'numpy.ndarray'>\n",
      "oneArray 内容: [1 2 3 4]\n",
      "t1 内容: [1 2 3]\n",
      "t1 类型: <class 'numpy.ndarray'>\n",
      "t2 内容: [0 1 2 3 4 5 6 7 8 9]\n",
      "t2 类型: <class 'numpy.ndarray'>\n",
      "t3 内容: [0 2 4 6 8]\n",
      "t3 类型: <class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T13:27:35.419678Z",
     "start_time": "2025-03-09T13:27:35.414164Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 定义一个嵌套列表\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "\n",
    "# 将嵌套列表转换为 NumPy 数组\n",
    "twoArray = np.array(list2)\n",
    "\n",
    "print(\"twoArray 内容:\\n\", twoArray)\n",
    "\n",
    "print(twoArray.size)"
   ],
   "id": "c96b6cd21fa8320b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "twoArray 内容:\n",
      " [[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "6\n"
     ]
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-08T14:20:44.208423Z",
     "start_time": "2025-03-08T14:20:44.197146Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "list2 = [[1, 2], [3, 4], [5, 6]]\n",
    "\n",
    "# 将嵌套列表转换为 NumPy 数组\n",
    "twoArray = np.array(list2)\n",
    "\n",
    "# 获取数组的维度（注意：与函数的参数很像）\n",
    "print(\"数组的维度:\", twoArray.ndim)\n",
    "\n",
    "# 获取数组的形状（行，列）\n",
    "print(\"数组的形状:\", twoArray.shape)\n",
    "\n",
    "# 获取数组的元素个数\n",
    "print(\"数组的元素个数:\", twoArray.size)\n"
   ],
   "id": "3701edb2eb876335",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的维度: 2\n",
      "数组的形状: (3, 2)\n",
      "数组的元素个数: 6\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:28:59.559471Z",
     "start_time": "2025-03-09T05:28:59.548154Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "four = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "# 修改数组形状（原地修改）\n",
    "four.shape = (3, 2)\n",
    "print(\"After reshaping in-place:\")\n",
    "print(four)\n",
    "\n",
    "# 返回一个新的数组（非原地修改）\n",
    "four = four.reshape(3, 2)\n",
    "print(\"\\nAfter reshaping (new array):\")\n",
    "print(four)\n",
    "\n",
    "# 将多维数组转换为一维数组\n",
    "five = four.reshape((6,), order='F')  # 按列优先顺序展开\n",
    "six = four.flatten(order='F')  # 按列优先顺序展平\n",
    "print(\"\\nReshaped to 1D (column-major):\")\n",
    "print(five)\n",
    "print(\"\\nFlattened to 1D (column-major):\")\n",
    "print(six)\n",
    "\n",
    "# 拓展：数组的形状\n",
    "t = np.arange(24)\n",
    "print(\"\\nInitial 1D array:\")\n",
    "print(t)\n",
    "print(\"Shape of t:\", t.shape)\n",
    "\n",
    "# 将一维数组转换为二维数组\n",
    "t1 = t.reshape((4, 6))\n",
    "print(\"\\nReshaped to 2D:\")\n",
    "print(t1)\n",
    "print(\"Shape of t1:\", t1.shape)"
   ],
   "id": "94e66b35ca3a1501",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After reshaping in-place:\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "After reshaping (new array):\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "\n",
      "Reshaped to 1D (column-major):\n",
      "[1 3 5 2 4 6]\n",
      "\n",
      "Flattened to 1D (column-major):\n",
      "[1 3 5 2 4 6]\n",
      "\n",
      "Initial 1D array:\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]\n",
      "Shape of t: (24,)\n",
      "\n",
      "Reshaped to 2D:\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "Shape of t1: (4, 6)\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T13:26:56.426991Z",
     "start_time": "2025-03-09T13:26:56.421819Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "b = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(b.size)\n",
    "# 将多维数组转换为 Python 列表\n",
    "list_b = b.tolist()\n",
    "\n",
    "# 输出列表及其类型\n",
    "print(\"List:\", list_b)\n",
    "print(\"Type of list_b:\", type(list_b))"
   ],
   "id": "ffe9847e994cad91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "List: [[1, 2, 3], [4, 5, 6]]\n",
      "Type of list_b: <class 'list'>\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:35:57.834261Z",
     "start_time": "2025-03-09T05:35:57.824801Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import random\n",
    "\n",
    "# 返回数组中每个元素的字节单位长度，dtype 设置数据类型\n",
    "f = np.array([1, 2, 3, 4, 5], dtype=np.int16)\n",
    "\n",
    "# 打印每个元素的字节大小\n",
    "print(\"Item size of f:\", f.itemsize)  # 输出 2，因为 np.int16 占用 2 个字节\n",
    "\n",
    "# 获取数据类型\n",
    "print(\"Data type of f:\", f.dtype)  # 输出 int16\n",
    "\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)  # 将数据类型转换为 np.int64\n",
    "print(\"Data type of f1:\", f1.dtype)  # 输出 int64\n",
    "\n",
    "# 拓展：随机生成小数并保留两位\n",
    "# 使用 Python 语法，保留两位\n",
    "random_float = round(random.random(), 2)\n",
    "print(\"Random float (Python):\", random_float)\n",
    "\n",
    "# 使用 NumPy 生成随机数组\n",
    "arr = np.array([random.random() for i in range(10)])  # 生成包含 10 个随机小数的数组\n",
    "\n",
    "# 取小数点后两位\n",
    "arr_rounded = np.round(arr, 2)\n",
    "print(\"Rounded array (NumPy):\", arr_rounded)"
   ],
   "id": "5472e80318200b3e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Item size of f: 2\n",
      "Data type of f: int16\n",
      "Data type of f1: int64\n",
      "Random float (Python): 0.72\n",
      "Rounded array (NumPy): [0.35 0.58 0.14 0.72 0.02 0.34 0.63 0.82 0.86 0.15]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:46:56.491445Z",
     "start_time": "2025-03-09T05:46:56.482893Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "print(\"Original array t1:\")\n",
    "print(t1)\n",
    "\n",
    "# 数组与标量相加\n",
    "print(\"\\nt1 + 2:\")\n",
    "print(t1 + 2)\n",
    "\n",
    "print(\"-\" * 20)\n",
    "\n",
    "# 数组与标量相乘\n",
    "print(\"\\nt1 * 2:\")\n",
    "print(t1 * 2)\n",
    "\n",
    "print(\"-\" * 20)\n",
    "\n",
    "# 数组与标量相除\n",
    "print(\"\\nt1 / 2:\")\n",
    "print(t1 / 2)\n",
    "\n"
   ],
   "id": "c28adca0700c2883",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original array t1:\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "\n",
      "t1 + 2:\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "--------------------\n",
      "\n",
      "t1 * 2:\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "--------------------\n",
      "\n",
      "t1 / 2:\n",
      "[[ 0.   0.5  1.   1.5]\n",
      " [ 2.   2.5  3.   3.5]\n",
      " [ 4.   4.5  5.   5.5]\n",
      " [ 6.   6.5  7.   7.5]\n",
      " [ 8.   8.5  9.   9.5]\n",
      " [10.  10.5 11.  11.5]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:51:46.656365Z",
     "start_time": "2025-03-09T05:51:46.647889Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(100, 124).reshape((6, 4))\n",
    "\n",
    "print(\"Array t1:\")\n",
    "print(t1)\n",
    "print(\"\\nArray t2:\")\n",
    "print(t2)\n",
    "\n",
    "# 数组逐元素相加\n",
    "print(\"\\nt1 + t2:\")\n",
    "print(t1 + t2)\n",
    "\n",
    "# 数组逐元素相减\n",
    "print(\"\\nt1 - t2:\")\n",
    "print(t1 - t2)"
   ],
   "id": "e52fac94e67a6b46",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array t1:\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "\n",
      "Array t2:\n",
      "[[100 101 102 103]\n",
      " [104 105 106 107]\n",
      " [108 109 110 111]\n",
      " [112 113 114 115]\n",
      " [116 117 118 119]\n",
      " [120 121 122 123]]\n",
      "\n",
      "t1 + t2:\n",
      "[[100 102 104 106]\n",
      " [108 110 112 114]\n",
      " [116 118 120 122]\n",
      " [124 126 128 130]\n",
      " [132 134 136 138]\n",
      " [140 142 144 146]]\n",
      "\n",
      "t1 - t2:\n",
      "[[-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]\n",
      " [-100 -100 -100 -100]]\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:55:02.478245Z",
     "start_time": "2025-03-09T05:55:02.449473Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#不种形状的多维数组不能计算\n",
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(18).reshape((6, 3))\n",
    "\n",
    "print(\"\\nt1 - t2:\")\n",
    "print(t1 - t2)"
   ],
   "id": "279b0ed6f0ff920d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "t1 - t2:\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (4,6) (6,3) ",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[10], line 6\u001B[0m\n\u001B[1;32m      3\u001B[0m t2 \u001B[38;5;241m=\u001B[39m np\u001B[38;5;241m.\u001B[39marange(\u001B[38;5;241m18\u001B[39m)\u001B[38;5;241m.\u001B[39mreshape((\u001B[38;5;241m6\u001B[39m, \u001B[38;5;241m3\u001B[39m))\n\u001B[1;32m      5\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;130;01m\\n\u001B[39;00m\u001B[38;5;124mt1 - t2:\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[0;32m----> 6\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[43mt1\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m-\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mt2\u001B[49m)\n",
      "\u001B[0;31mValueError\u001B[0m: operands could not be broadcast together with shapes (4,6) (6,3) "
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:58:28.878195Z",
     "start_time": "2025-03-09T05:58:28.870103Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#行数或者列数相同的一维数组和多维数组可以进行计算：\n",
    "t1 = np.arange(24).reshape((6, 4))\n",
    "t2 = np.arange(4).reshape((1,4))\n",
    "\n",
    "print(t1)\n",
    "print(t2)\n",
    "\n",
    "print(t1 - t2)"
   ],
   "id": "28bf89fa259e5640",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "[[0 1 2 3]]\n",
      "[[ 0  0  0  0]\n",
      " [ 4  4  4  4]\n",
      " [ 8  8  8  8]\n",
      " [12 12 12 12]\n",
      " [16 16 16 16]\n",
      " [20 20 20 20]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:59:15.763286Z",
     "start_time": "2025-03-09T05:59:15.759139Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape((4, 6))\n",
    "t2 = np.arange(4).reshape((4,1))"
   ],
   "id": "ab3fb5fa5deed379",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T05:59:21.325644Z",
     "start_time": "2025-03-09T05:59:21.320009Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "print(t1)\n",
    "print(t2)\n",
    "\n",
    "print(t1 - t2)"
   ],
   "id": "b9b45055d53b5efe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[0]\n",
      " [1]\n",
      " [2]\n",
      " [3]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T06:06:18.282352Z",
     "start_time": "2025-03-09T06:06:18.269713Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "#按哪个轴运算，哪个轴就会消失\n",
    "\n",
    "# 沿 axis=0 求和（按列求和）\n",
    "print(\"Sum along axis=0:\")\n",
    "print(np.sum(a, axis=0))  # 输出 [5 7 9]\n",
    "\n",
    "# 沿 axis=1 求和（按行求和）\n",
    "print(\"\\nSum along axis=1:\")\n",
    "print(np.sum(a, axis=1))  # 输出 [6 15]\n",
    "\n",
    "# 对整个数组求和\n",
    "print(\"\\nSum of the entire array:\")\n",
    "print(np.sum(a))  # 输出 21\n",
    "\n",
    "#最外层括号是0轴，最内层括号是最大轴\n",
    "a = np.arange(27).reshape((3, 3, 3))\n",
    "print(\"\\n3D array a:\")\n",
    "print(a)\n",
    "\n",
    "# 沿 axis=0 求和\n",
    "b = np.sum(a, axis=0)\n",
    "print(\"\\nSum along axis=0:\")\n",
    "print(b)\n",
    "\n",
    "# 分隔线\n",
    "print(\"-\" * 20)\n",
    "\n",
    "# 沿 axis=1 求和\n",
    "c = np.sum(a, axis=1)\n",
    "print(\"\\nSum along axis=1:\")\n",
    "print(c)\n",
    "\n",
    "# 分隔线\n",
    "print(\"-\" * 20)\n",
    "\n",
    "# 沿 axis=2 求和\n",
    "d = np.sum(a, axis=2)\n",
    "print(\"\\nSum along axis=2:\")\n",
    "print(d)"
   ],
   "id": "f1f62056c86e4269",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sum along axis=0:\n",
      "[5 7 9]\n",
      "\n",
      "Sum along axis=1:\n",
      "[ 6 15]\n",
      "\n",
      "Sum of the entire array:\n",
      "21\n",
      "\n",
      "3D array a:\n",
      "[[[ 0  1  2]\n",
      "  [ 3  4  5]\n",
      "  [ 6  7  8]]\n",
      "\n",
      " [[ 9 10 11]\n",
      "  [12 13 14]\n",
      "  [15 16 17]]\n",
      "\n",
      " [[18 19 20]\n",
      "  [21 22 23]\n",
      "  [24 25 26]]]\n",
      "\n",
      "Sum along axis=0:\n",
      "[[27 30 33]\n",
      " [36 39 42]\n",
      " [45 48 51]]\n",
      "--------------------\n",
      "\n",
      "Sum along axis=1:\n",
      "[[ 9 12 15]\n",
      " [36 39 42]\n",
      " [63 66 69]]\n",
      "--------------------\n",
      "\n",
      "Sum along axis=2:\n",
      "[[ 3 12 21]\n",
      " [30 39 48]\n",
      " [57 66 75]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T06:16:40.835158Z",
     "start_time": "2025-03-09T06:16:40.827960Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "print(\"Array a:\", a)\n",
    "\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作\n",
    "print(a[2:7:2])  # 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[2])  # 返回索引 2 对应的元素\n",
    "\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])  # 从索引 2 开始提取所有元素"
   ],
   "id": "85ddd860f1639a77",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array a: [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 4 6]\n",
      "2\n",
      "[2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T09:27:40.878531Z",
     "start_time": "2025-03-09T09:27:40.863450Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(\"Original array t1:\")\n",
    "print(t1)\n",
    "\n",
    "# 取一行\n",
    "print(\"\\nRow at index 1:\")\n",
    "print(t1[0])  # 取第 0 行\n",
    "print(t1[1, :])  # 取第 1 行（等价于 t1[1]）\n",
    "\n",
    "# 取连续的多行\n",
    "print(\"\\nRows from index 1 onwards:\")\n",
    "print(t1[1:])  # 从第 1 行开始取所有行\n",
    "\n",
    "print(\"\\nRows from index 1 to 2:\")\n",
    "print(t1[1:3, :])  # 取第 1 行到第 2 行\n",
    "\n",
    "# 取不连续的多行\n",
    "print(\"\\nRows at indices 0, 2, 3:\")\n",
    "print(t1[[0, 2, 3]])  # 取第 0、2、3 行\n",
    "print(t1[[0, 2, 3], :])  # 取第 0、2、3 行（等价于 t1[[0, 2, 3]]）\n",
    "\n",
    "# 取一列\n",
    "print(\"\\nColumn at index 1:\")\n",
    "print(t1[:, 1])  # 取第 1 列\n",
    "\n",
    "# 取连续的多列\n",
    "print(\"\\nColumns from index 1 onwards:\")\n",
    "print(t1[:, 1:])  # 从第 1 列开始取所有列\n",
    "\n",
    "# 取不连续的多列\n",
    "print(\"\\nColumns at indices 0, 2, 3:\")\n",
    "print(t1[:, [0, 2, 3]])  # 取第 0、2、3 列\n",
    "\n",
    "# 取单个元素\n",
    "print(\"\\nElement at row 2, column 3:\")\n",
    "print(t1[2, 3])  # 取第 2 行第 3 列的元素\n",
    "print()\n",
    "\n",
    "print('-'*50)\n",
    "print(t1[[1,2],[0,2]])# 取第1行第0列和第2行第2列的元素"
   ],
   "id": "15820778a573bca8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original array t1:\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "Row at index 1:\n",
      "[0 1 2 3 4 5]\n",
      "[ 6  7  8  9 10 11]\n",
      "\n",
      "Rows from index 1 onwards:\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "Rows from index 1 to 2:\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "\n",
      "Rows at indices 0, 2, 3:\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "Column at index 1:\n",
      "[ 1  7 13 19]\n",
      "\n",
      "Columns from index 1 onwards:\n",
      "[[ 1  2  3  4  5]\n",
      " [ 7  8  9 10 11]\n",
      " [13 14 15 16 17]\n",
      " [19 20 21 22 23]]\n",
      "\n",
      "Columns at indices 0, 2, 3:\n",
      "[[ 0  2  3]\n",
      " [ 6  8  9]\n",
      " [12 14 15]\n",
      " [18 20 21]]\n",
      "\n",
      "Element at row 2, column 3:\n",
      "15\n",
      "\n",
      "--------------------------------------------------\n",
      "[ 6 14]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T09:56:30.179189Z",
     "start_time": "2025-03-09T09:56:30.167368Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "t = np.arange(24).reshape(4, 6)\n",
    "print(\"Original array t:\")\n",
    "print(t)\n",
    "\n",
    "# 修改某一行的值\n",
    "t[1, :] = 0\n",
    "print(\"\\nAfter modifying row at index 1:\")\n",
    "print(t)\n",
    "\n",
    "# 修改某一列的值\n",
    "t[:, 1] = 0\n",
    "print(\"\\nAfter modifying column at index 1:\")\n",
    "print(t)\n",
    "\n",
    "# 修改连续多行\n",
    "t[1:3, :] = 0\n",
    "print(\"\\nAfter modifying rows from index 1 to 2:\")\n",
    "print(t)\n",
    "\n",
    "# 修改连续多列\n",
    "t[:, 1:4] = 0\n",
    "print(\"\\nAfter modifying columns from index 1 to 3:\")\n",
    "print(t)\n",
    "\n",
    "# 修改多行多列，取第二行到第四行，第三列到第五列\n",
    "t[1:4, 2:5] = 0\n",
    "print(\"\\nAfter modifying rows 1:4 and columns 2:5:\")\n",
    "print(t)\n",
    "\n"
   ],
   "id": "510a1f46866d23b2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original array t:\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "After modifying row at index 1:\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "After modifying column at index 1:\n",
      "[[ 0  0  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [12  0 14 15 16 17]\n",
      " [18  0 20 21 22 23]]\n",
      "\n",
      "After modifying rows from index 1 to 2:\n",
      "[[ 0  0  2  3  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0 20 21 22 23]]\n",
      "\n",
      "After modifying columns from index 1 to 3:\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0 22 23]]\n",
      "\n",
      "After modifying rows 1:4 and columns 2:5:\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:02:18.334528Z",
     "start_time": "2025-03-09T10:02:18.320498Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 修改多个不相邻的点\n",
    "t[[0, 1], [0, 3]] = 0\n",
    "print(\"\\nAfter modifying specific points:\")\n",
    "print(t)\n",
    "\n",
    "# 根据条件修改，比如将小于 10 的值改为 0\n",
    "t[t < 10] = 0\n",
    "print(\"\\nAfter modifying values less than 10:\")\n",
    "print(t)\n",
    "\n",
    "# 使用逻辑判断\n",
    "t[(t > 2) & (t < 6)] = 0  # 与\n",
    "print(\"\\nAfter modifying values between 2 and 6:\")\n",
    "print(t)\n",
    "\n",
    "t[(t < 2) | (t > 6)] = 0  # 或\n",
    "print(\"\\nAfter modifying values less than 2 or greater than 6:\")\n",
    "print(t)\n",
    "\n",
    "t[~(t > 6)] = 0  # 非\n",
    "print(\"\\nAfter modifying values not greater than 6:\")\n",
    "print(t)\n",
    "\n"
   ],
   "id": "eec4c3fb8f4b6d4c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "After modifying specific points:\n",
      "[[ 0  0  0  0  4  5]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "\n",
      "After modifying values less than 10:\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "\n",
      "After modifying values between 2 and 6:\n",
      "[[ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [ 0  0  0  0  0  0]\n",
      " [18  0  0  0  0 23]]\n",
      "\n",
      "After modifying values less than 2 or greater than 6:\n",
      "[[0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n",
      "\n",
      "After modifying values not greater than 6:\n",
      "[[0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n",
      "\n",
      "After clipping values between 10 and 18:\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]\n",
      " [10 10 10 10 10 10]]\n",
      "\n",
      "Result of np.where:\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:07:08.775683Z",
     "start_time": "2025-03-09T10:07:08.768005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4, 6)\n",
    "print(\"Original array t1:\")\n",
    "print(t1)\n",
    "\n",
    "# 使用 clip 方法将数组中的值限制在 10 到 18 之间\n",
    "t = t1.clip(10, 18)\n",
    "print(\"\\nAfter clipping values between 10 and 18:\")\n",
    "print(t)\n",
    "\n"
   ],
   "id": "4df833158cf1c15d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original array t1:\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "\n",
      "After clipping values between 10 and 18:\n",
      "[[10 10 10 10 10 10]\n",
      " [10 10 10 10 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 18 18 18 18 18]]\n",
      "\n",
      "Result of np.where:\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:08:27.830586Z",
     "start_time": "2025-03-09T10:08:27.825826Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 拓展：三目运算（np.where）\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "result = np.where(score > 80, True, False)\n",
    "print(\"\\nResult of np.where:\")\n",
    "print(result)"
   ],
   "id": "3c3e446bb5c6a077",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Result of np.where:\n",
      "[[False  True]\n",
      " [ True  True]\n",
      " [False  True]]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:16:56.986365Z",
     "start_time": "2025-03-09T10:16:56.978313Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(\"第一个数组：\")\n",
    "print(a)\n",
    "\n",
    "# 向数组添加元素\n",
    "print(\"\\n向数组添加元素：\")\n",
    "print(np.append(a, [7, 8, 9]))  # 将 [7, 8, 9] 添加到数组末尾，结果为一维数组\n",
    "\n",
    "# 沿轴 0 添加元素\n",
    "print(\"\\n沿轴 0 添加元素：\")\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))  # 沿行方向（轴 0）添加一行 [7, 8, 9]\n",
    "\n",
    "# 沿轴 1 添加元素\n",
    "print(\"\\n沿轴 1 添加元素：\")\n",
    "print(np.append(a, [[5, 5, 5], [7, 8, 9]], axis=1))  # 沿列方向（轴 1）添加两列 [5, 5, 5] 和 [7, 8, 9]"
   ],
   "id": "de12ba4f08337616",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "向数组添加元素：\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "沿轴 0 添加元素：\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "\n",
      "沿轴 1 添加元素：\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:28:18.459935Z",
     "start_time": "2025-03-09T10:28:18.447466Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "a = np.array([[1, 2], [3, 4], [5, 6]])\n",
    "print(\"第一个数组：\")\n",
    "print(a)\n",
    "\n",
    "# 未传递 Axis 参数。在插入之前输入数组会被展开。\n",
    "print(np.insert(a, 3, [11, 12]))  # 在展开后的数组的第 3 个位置插入 [11, 12]\n",
    "\n",
    "# 传递了 Axis 参数。会广播值数组来配输入数组。\n",
    "\n",
    "# 沿轴 0 广播\n",
    "print(\"\\n沿轴 0 广播：\")\n",
    "print(np.insert(a, 1, 11, axis=0))  # 在第 1 行插入 [11, 11]\n",
    "\n",
    "# 沿轴 1 广播\n",
    "print(\"\\n沿轴 1 广播：\")\n",
    "print(np.insert(a, 1, [11], axis=1))  # 在第 1 列插入 11"
   ],
   "id": "a47e1bccb1348fd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "[ 1  2  3 11 12  4  5  6]\n",
      "\n",
      "沿轴 0 广播：\n",
      "[[ 1  2]\n",
      " [11 11]\n",
      " [ 3  4]\n",
      " [ 5  6]]\n",
      "\n",
      "沿轴 1 广播：\n",
      "[[ 1 11  2]\n",
      " [ 3 11  4]\n",
      " [ 5 11  6]]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:30:21.081778Z",
     "start_time": "2025-03-09T10:30:21.075253Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print(\"第一个数组：\")\n",
    "print(a)\n",
    "\n",
    "# 未传递 Axis 参数。在删除之前输入数组会被展开。\n",
    "print(\"\\n未传递 Axis 参数。在删除之前输入数组会被展开：\")\n",
    "print(np.delete(a, 5))  # 删除展开后的一维数组的第 5 个元素\n",
    "\n",
    "# 删除每一行中的第二列\n",
    "print(\"\\n删除每一行中的第二列：\")\n",
    "print(np.delete(a, 1, axis=1))  # 沿列方向（轴 1）删除第 1 列"
   ],
   "id": "1deeea8dff2bd630",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "\n",
      "未传递 Axis 参数。在删除之前输入数组会被展开：\n",
      "[ 0  1  2  3  4  6  7  8  9 10 11]\n",
      "\n",
      "删除每一行中的第二列：\n",
      "[[ 0  2  3]\n",
      " [ 4  6  7]\n",
      " [ 8 10 11]]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:33:01.928982Z",
     "start_time": "2025-03-09T10:33:01.918125Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "a = np.array([5, 2, 6, 2, 7, 5, 6, 8, 2, 9])\n",
    "print(\"第一个数组：\")\n",
    "print(a)\n",
    "\n",
    "# 获取数组的去重值\n",
    "print(\"\\n第一个数组的去重值：\")\n",
    "u = np.unique(a)\n",
    "print(u)\n",
    "\n",
    "# 获取去重数组的索引数组\n",
    "print(\"\\n去重数组的索引数组：\")\n",
    "u, indices = np.unique(a, return_index=True)\n",
    "print(indices)\n",
    "\n",
    "# 获取去重数组的下标\n",
    "print(\"\\n我们可以看到每个和原数组下标对应的数值：\")\n",
    "print(a)\n",
    "print(\"去重数组的下标：\")\n",
    "u, indices = np.unique(a, return_inverse=True)\n",
    "print(u)\n",
    "print(indices)\n",
    "\n",
    "# 返回去重元素的重复数量\n",
    "print(\"\\n返回去重元素的重复数量：\")\n",
    "u, indices = np.unique(a, return_counts=True)\n",
    "print(u)\n",
    "print(indices)"
   ],
   "id": "27f51b1e5868d9f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "\n",
      "第一个数组的去重值：\n",
      "[2 5 6 7 8 9]\n",
      "\n",
      "去重数组的索引数组：\n",
      "[1 0 2 4 7 9]\n",
      "\n",
      "我们可以看到每个和原数组下标对应的数值：\n",
      "[5 2 6 2 7 5 6 8 2 9]\n",
      "去重数组的下标：\n",
      "[2 5 6 7 8 9]\n",
      "[1 0 2 0 3 1 2 4 0 5]\n",
      "\n",
      "返回去重元素的重复数量：\n",
      "[2 5 6 7 8 9]\n",
      "[3 2 2 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:38:44.007647Z",
     "start_time": "2025-03-09T10:38:43.991677Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "\n",
    "# 1. 获取所有数据最大值\n",
    "result = np.max(score)\n",
    "print(\"所有数据的最大值：\")\n",
    "print(result)\n",
    "\n",
    "# 2. 获取某一个轴上的数据最大值\n",
    "result = np.max(score, axis=0)\n",
    "print(\"\\n沿轴 0 的最大值（每列的最大值）：\")\n",
    "print(result)\n",
    "\n",
    "# 3. 获取最小值\n",
    "result = np.min(score)\n",
    "print(\"\\n所有数据的最小值：\")\n",
    "print(result)\n",
    "\n",
    "# 4. 获取某一个轴上的数据最小值\n",
    "result = np.min(score, axis=0)\n",
    "print(\"\\n沿轴 0 的最小值（每列的最小值）：\")\n",
    "print(result)\n",
    "\n"
   ],
   "id": "6d4e5f1f0657b25f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有数据的最大值：\n",
      "88\n",
      "\n",
      "沿轴 0 的最大值（每列的最大值）：\n",
      "[82 88]\n",
      "\n",
      "所有数据的最小值：\n",
      "75\n",
      "\n",
      "沿轴 0 的最小值（每列的最小值）：\n",
      "[75 81]\n",
      "\n",
      "np.maximum([-2, -1, 0, 1, 2], 0)：\n",
      "[0 0 0 1 2]\n",
      "\n",
      "np.minimum([-2, -1, 0, 1, 2], 0)：\n",
      "[-2 -1  0  0  0]\n",
      "\n",
      "np.maximum([-2, -1, 0, 1, 2], [1, 2, 3, 4, 5])：\n",
      "[1 2 3 4 5]\n",
      "\n",
      "所有数据的平均值：\n",
      "81.16666666666667\n",
      "\n",
      "沿轴 0 的平均值（每列的平均值）：\n",
      "[79.         83.33333333]\n",
      "\n",
      "原始数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "沿轴 0 的前缀和：\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:43:51.519981Z",
     "start_time": "2025-03-09T10:43:51.507683Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 5. 数据的比较\n",
    "result = np.maximum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回大的\n",
    "print(\"\\nnp.maximum([-2, -1, 0, 1, 2], 0)：\")\n",
    "print(result)\n",
    "\n",
    "result = np.minimum([-2, -1, 0, 1, 2], 0)  # 第一个参数中的每一个数与第二个参数比较返回小的\n",
    "print(\"\\nnp.minimum([-2, -1, 0, 1, 2], 0)：\")\n",
    "print(result)\n",
    "\n",
    "result = np.maximum([-2, -1, 0, 1, 2], [1, 2, 3, 4, 5])  # 两个参数大小一致，逐元素比较\n",
    "print(\"\\nnp.maximum([-2, -1, 0, 1, 2], [1, 2, 3, 4, 5])：\")\n",
    "print(result)\n",
    "\n",
    "# 6. 求平均值\n",
    "result = np.mean(score)  # 获取所有数据的平均值\n",
    "print(\"\\n所有数据的平均值：\")\n",
    "print(result)\n",
    "\n",
    "result = np.mean(score, axis=0)  # 获取沿轴 0 的平均值（每列的平均值）\n",
    "print(\"\\n沿轴 0 的平均值（每列的平均值）：\")\n",
    "print(result)\n",
    "\n",
    "# 7. 求前缀和\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(\"\\n原始数组：\")\n",
    "print(arr)\n",
    "\n",
    "print(\"\\n沿轴 0 的前缀和：\")\n",
    "print(arr.cumsum(0))  # 沿轴 0 的前缀和\n",
    "print(\"\\n沿轴 1 的前缀和：\")\n",
    "print(arr.cumsum(1))"
   ],
   "id": "ca429d2d5468aa57",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "np.maximum([-2, -1, 0, 1, 2], 0)：\n",
      "[0 0 0 1 2]\n",
      "\n",
      "np.minimum([-2, -1, 0, 1, 2], 0)：\n",
      "[-2 -1  0  0  0]\n",
      "\n",
      "np.maximum([-2, -1, 0, 1, 2], [1, 2, 3, 4, 5])：\n",
      "[1 2 3 4 5]\n",
      "\n",
      "所有数据的平均值：\n",
      "81.16666666666667\n",
      "\n",
      "沿轴 0 的平均值（每列的平均值）：\n",
      "[79.         83.33333333]\n",
      "\n",
      "原始数组：\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "沿轴 0 的前缀和：\n",
      "[[1 2 3]\n",
      " [5 7 9]]\n",
      "\n",
      "沿轴 1 的前缀和：\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-09T10:45:12.118012Z",
     "start_time": "2025-03-09T10:45:12.110044Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "\n",
    "# 获取每一列的最小值及其索引\n",
    "result = np.argmin(score, axis=0)  # 返回每一列最小值的索引\n",
    "res = np.min(score, axis=0)  # 返回每一列的最小值\n",
    "print(\"每一列最小值的索引和最小值：\")\n",
    "print(result, res)\n",
    "\n",
    "# 求每一列的标准差（总体标准差）\n",
    "result = np.std(score, axis=0)\n",
    "print(\"\\n每一列的标准差：\")\n",
    "print(result)\n",
    "\n",
    "# 求极值（最大值和最小值的差）\n",
    "result = np.ptp(score, axis=None)\n",
    "print(\"\\n极值（最大值和最小值的差）：\")\n",
    "print(result)"
   ],
   "id": "4adabda740015c47",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "每一列最小值的索引和最小值：\n",
      "[2 1] [75 81]\n",
      "\n",
      "每一列的标准差：\n",
      "[2.94392029 3.29983165]\n",
      "\n",
      "极值（最大值和最小值的差）：\n",
      "13\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "469a40b768652818"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
